<!DOCTYPE html>
<html lang="cn">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="小马向前走">
<meta property="og:type" content="website">
<meta property="og:title" content="小马向前走">
<meta property="og:url" content="http://example.com/page/2/index.html">
<meta property="og:site_name" content="小马向前走">
<meta property="og:description" content="小马向前走">
<meta property="og:locale">
<meta property="article:author" content="maning">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/page/2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'cn'
  };
</script>

  <title>小马向前走</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">小马向前走</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content index posts-expand">
            
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="cn">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/12/08-restclient/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="maning">
      <meta itemprop="description" content="小马向前走">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小马向前走">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/07/12/08-restclient/" class="post-title-link" itemprop="url">REST Client 插件的使用</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2021-07-12 12:08:33" itemprop="dateCreated datePublished" datetime="2021-07-12T12:08:33+08:00">2021-07-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2022-02-24 17:33:24" itemprop="dateModified" datetime="2022-02-24T17:33:24+08:00">2022-02-24</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">工具</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="引言"><a href="#引言" class="headerlink" title="引言"></a>引言</h1><p>REST Client 是一个 VSCode 插件，使我们可以直接在 VSCode 中直接发送http请求，目前我用它来代替 postman 做一些简单接口测试。</p>
<p>REST Client allows you to send HTTP request and view the response in Visual Studio Code directly</p>
<p><img src="/2021/07/12/08-restclient/restClient-28456.png" alt="restClient-1"></p>
<p>说起测试接口，大家一定会想起 postman ，那在引入 REST Client 插件之前，需要解答一个问题，难道 postman 不香么，为什么还要引入新工具? 实际上 postman 很香，只是对于一些入参简单的请求，用 postman 感觉太重了，而且如果接口数量很多，导致打开很多标签。</p>
<p><strong>一直以来我也在寻找更好的方案，一个不用切换窗口的方案</strong>  – 终于在使用 VSCode 一段时间后，我找到了 REST Client 插件，初看 REST Client 插件的时候，会觉得他十分简陋，但是在使用一段时间后会发现在 REST Client 插件中已经有完成接口测试所需的大部分功能。</p>
<p><strong>我总结了3点使用REST Client插件的好处</strong></p>
<ol>
<li>便于分享，因为是纯文本，联调时可以做为一个成果物，发给前端开发同事做为测试列表，这是我使用它的主要原因</li>
<li>不用切换标签，通过###分隔符，同一个HTTP文件里可以包含多个HTTP请求， 不像Postman，不同的HTTP请求需要放在不同的tab里</li>
<li>可以更直接的接触到http的相关知识，因为不论请求还是返回都是文本，这样可以更直接的感受到http请求的细节</li>
</ol>
<h1 id="相关资料"><a href="#相关资料" class="headerlink" title="相关资料"></a>相关资料</h1><p>github </p>
<p><a target="_blank" rel="noopener" href="https://github.com/Huachao/vscode-restclient">https://github.com/Huachao/vscode-restclient</a></p>
<h1 id="关于安装"><a href="#关于安装" class="headerlink" title="关于安装"></a>关于安装</h1><p>直接在插件商店中搜索 REST Client 安装即可</p>
<p><img src="/2021/07/12/08-restclient/restClient-13246.png" alt="restClient-13246"></p>
<h1 id="简单使用"><a href="#简单使用" class="headerlink" title="简单使用"></a>简单使用</h1><p>本节列出几个示例，指引大家快速入门</p>
<h2 id="示例1-get请求示例"><a href="#示例1-get请求示例" class="headerlink" title="示例1 get请求示例"></a>示例1 get请求示例</h2><p>这里演示如何发送一个get请求，从该示例中你会掌握基本的用法</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@IP=127.0.0.1:8083</span><br><span class="line"></span><br><span class="line">### get 请求示例</span><br><span class="line">GET http://&#123;&#123;IP&#125;&#125;/helloJson/testParam?name=hello</span><br></pre></td></tr></table></figure>

<p>当一个文件的后缀名是.http 或者 .rest时，VSCode 会自动识别成接口文件</p>
<p>在VSCode中 <strong>用三个#号 ### 表示一个接口的开始</strong>，其下方会有一个 Send Request的按钮，当点击该按钮时会发送请求，之后会在窗口右边显示请求返回的内容</p>
<p><img src="/2021/07/12/08-restclient/restClient-26417.png" alt="restClient-26417"></p>
<h2 id="示例2-post请求示例"><a href="#示例2-post请求示例" class="headerlink" title="示例2 post请求示例"></a>示例2 post请求示例</h2><p>这里演示如何发送一个post请求</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@IP=127.0.0.1:8083</span><br><span class="line"></span><br><span class="line">### post 请求示例</span><br><span class="line">POST &#123;&#123;IP&#125;&#125;/authorize/register</span><br><span class="line"><span class="attribute">Content-Type</span>: application/json</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;name&quot;: &quot;小天&quot;，</span><br><span class="line">    &quot;username&quot;: &quot;sky&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>注意data 与请求头之间有空行</strong></p>
<h1 id="高阶使用"><a href="#高阶使用" class="headerlink" title="高阶使用"></a>高阶使用</h1><p>其实REST Client还有很多的功能，有需求的同学可以慢慢挖掘，笔者列出了一些比较有用的高阶技巧。</p>
<h2 id="示例1-变量的使用"><a href="#示例1-变量的使用" class="headerlink" title="示例1 变量的使用"></a>示例1 变量的使用</h2><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@IP=127.0.0.1:8083</span><br><span class="line"></span><br><span class="line">### get 请求示例</span><br><span class="line">GET http://&#123;&#123;IP&#125;&#125;/pgp/helloJson/testParam?name=hello</span><br></pre></td></tr></table></figure>

<p>实际上细心的你可能已经发现，在前面的简单示例中IP并没有写死，而是使用了一个变量，这样做的好处是可以快速切换环境。</p>
<p>当然这里的变量可以不光是变量，还可以是环境变量，其在使用方式上没有差别。</p>
<h2 id="示例2-Get请求参数写成多行"><a href="#示例2-Get请求参数写成多行" class="headerlink" title="示例2 Get请求参数写成多行"></a>示例2 Get请求参数写成多行</h2><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">### get 请求 参数写到多行示例</span><br><span class="line">GET https://example.com/comments</span><br><span class="line">    ?page=2</span><br><span class="line">    &amp;pageSize=10</span><br></pre></td></tr></table></figure>

<p>如果参数有很多，写在一行上不方便阅读和修改，则可以像这里一样分行写，一行一个。</p>
<h2 id="示例3-添加头信息"><a href="#示例3-添加头信息" class="headerlink" title="示例3 添加头信息"></a>示例3 添加头信息</h2><figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">### 测试 不拦截 请求头中加标记</span><br><span class="line">GET http://&#123;&#123;IP&#125;&#125;/pgp/helloJson/testMap</span><br><span class="line"><span class="attribute">XX-NOT-INTERCEPT:true</span></span><br></pre></td></tr></table></figure>

<p>如果需要添加自定义的头信息可以这样添加。</p>
<h2 id="示例4-添加cookie"><a href="#示例4-添加cookie" class="headerlink" title="示例4 添加cookie"></a>示例4 添加cookie</h2><figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">### 测试 cookie</span><br><span class="line">GET http://&#123;&#123;IP&#125;&#125;/pgp/helloJson/testCookie</span><br><span class="line"><span class="attribute">Cookie</span>: eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIxNjI2MTA1NjAwIiwic3ViIjoic2t5Iiwib2ZmaWNpYWxBY2NvdW50cyI6IuWwj-mprOWQkeWJjei1sCIsImlhdCI6MTYyNjE3MTcwMywiZXhwIjoxNjI2MTcyMjAzfQ.PTzL-lw6NQb2qhQg_-q2DWmMOe-1IcpEWcKyLv2rQfQhnrJiIEnVEd0s4FKxre5qVkyfc9b40u31dvJS-ejYUg</span><br></pre></td></tr></table></figure>

<p>这里演示在请求中如何添加cookie， 实际上cookie也是头的一部分，和添加头没什么区别。</p>
<h2 id="示例5-代码生成"><a href="#示例5-代码生成" class="headerlink" title="示例5 代码生成"></a>示例5 代码生成</h2><p>代码生成也是REST Client里一个很方便的功能，可以方便的将HTTP请求生成代码片段或者curl命令。</p>
<p><strong>Generate Code Snippet</strong></p>
<p>你可以方便地通过 Generate Code Snippet 命令来把HTTP请求生成出不同编程语言的代码：JavaScript， Python， C， C#， Java， PHP， Go， Ruby， Swift等等主流语言。</p>
<p><img src="/2021/07/12/08-restclient/restClient-21529.gif" alt="restClient-21529"></p>
<p><strong>Copy Request As cURL</strong></p>
<p>也可以通过Copy Request As cURL功能将HTTP请求生成 <code>curl</code> 命令，这个方法在运维排错时特别有用，在运维时有可能网络不通，需要登录到内网机器执行curl命令测试接口是否正常，但也许你对curl命令不熟悉， 不能很快写出命令，则可以借助REST Client 直接生成curl命令。</p>
<p><img src="/2021/07/12/08-restclient/restClient-128.png" alt="restClient-128"></p>
<p>选中接口文本–&gt; 右键 Copy Request As cURL</p>
<p><img src="/2021/07/12/08-restclient/restClient-25289.png" alt="restClient-25289"></p>
<p>如下是生成的curl命令</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">curl --request POST \</span><br><span class="line">  --url <span class="string">&#x27;http://127.0.0.1:8083/blue/sql/queryListByKeyWithParam?key=00_0005&#x27;</span> \</span><br><span class="line">  --header <span class="string">&#x27;content-type: application/json&#x27;</span> \</span><br><span class="line">  --header <span class="string">&#x27;user-agent: vscode-restclient&#x27;</span> \</span><br><span class="line">  --data <span class="string">&#x27;&#123;&quot;dataType&quot;:&quot;default&quot;,&quot;code&quot;:[&quot;001&quot;,&quot;002&quot;]&#125;&#x27;</span></span><br></pre></td></tr></table></figure>





<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>本节演示了REST Client的一些用法，希望大家把 VSCode 转化为一个 REST 接口测试的利器。</p>
<hr>
<p>微信端的朋友也可关注我的公众号</p>
<p><strong>小马向前走</strong></p>
<p><img src="/2021/07/12/08-restclient/qrcode-12cm.jpg" alt="qrcode-12cm"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="cn">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/02/08/07-rest-template/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="maning">
      <meta itemprop="description" content="小马向前走">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小马向前走">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/02/08/07-rest-template/" class="post-title-link" itemprop="url">RestTemplate在非springboot工程中的使用</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2021-02-08 15:34:58" itemprop="dateCreated datePublished" datetime="2021-02-08T15:34:58+08:00">2021-02-08</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2021-05-22 16:39:26" itemprop="dateModified" datetime="2021-05-22T16:39:26+08:00">2021-05-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="问题的引出"><a href="#问题的引出" class="headerlink" title="问题的引出"></a>问题的引出</h1><p>RestTemplate 是spring提供的访问rest接口的类，RestTemplate提供了多种便捷访问http服务的方法，使得调用rest接口更容易，记得在以前的代码中发送http请求我们还使用 HttpClient 或者 OkHttp 但其有如下的问题</p>
<ol>
<li>代码复杂</li>
<li>需要操心资源回收等细节问题</li>
<li>每个项目组都有自己的一套http工具类，重复造轮子</li>
</ol>
<p>现在我基本上用RestTemplate代替了以前自己写的工具类。</p>
<p><strong>本文不讲RestTemplate的普遍使用方法，而是讲在非springboot工程中如何使用RestTemplate</strong></p>
<p><strong>那为什么会有这样的场景呢？</strong>举个例子：我们要开发一个jar包提供给其他人，并集成到别人的工程中使用，该jar包封装对某短信接口的请求细节，别人只需要引入我们的jar包然后调用相关类即可，不用关心具体的http请求细节，因为我们提供的是一个很小的jar包，而不是一个完整的工程，所以再去用springboot那套就不合适了。</p>
<p>大部分情况下我们的工程都是基于springboot或者spring系的，使用RestTemplate很简单，直接自动注入即可。 </p>
<p><strong>那在脱离了spring系的工程中我们还能开心的使用RestTemplate么？</strong></p>
<h1 id="在非springboot工程中使用RestTemplate"><a href="#在非springboot工程中使用RestTemplate" class="headerlink" title="在非springboot工程中使用RestTemplate"></a>在非springboot工程中使用RestTemplate</h1><p><strong>步骤 pom.xml 中添加依赖</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.10.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>





<p><strong>步骤 代码中使用</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">RestTemplate restTemplate=<span class="keyword">new</span> RestTemplate();</span><br><span class="line">HttpHeaders headers=<span class="keyword">new</span> HttpHeaders();</span><br><span class="line">headers.setContentType(MediaType.APPLICATION_JSON_UTF8);</span><br><span class="line">HttpEntity requestEntity=<span class="keyword">new</span> HttpEntity&lt;&gt;(bean,headers);</span><br><span class="line">ResponseEntity&lt;String&gt; responseEntity = restTemplate.exchange(url, HttpMethod.POST,requestEntity,String.class);</span><br></pre></td></tr></table></figure>

<p>以前spring帮我们初始化RestTemplate，现在我们需要自己new 对象了，其它使用方式没有区别。</p>
<h1 id="关于初始化RestTemplate的说明"><a href="#关于初始化RestTemplate的说明" class="headerlink" title="关于初始化RestTemplate的说明"></a>关于初始化RestTemplate的说明</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">RestTemplate restTemplate = <span class="keyword">new</span> RestTemplate();</span><br><span class="line">RestTemplate restTemplate = <span class="keyword">new</span> RestTemplate(<span class="keyword">new</span> HttpComponentsClientHttpRequestFactory());</span><br><span class="line">RestTemplate restTemplate = <span class="keyword">new</span> RestTemplate(<span class="keyword">new</span> OkHttp3ClientHttpRequestFactory());</span><br></pre></td></tr></table></figure>

<p>实际上RestTemplate自己并不实现具体的http操作，其只是对已有类库进行了封装，而其具体实现仍然依赖底层的类库</p>
<ol>
<li>上面第一种初始化方法 其使用JDK自带的 HttpURLConnection</li>
<li>第二种初始化方法 使用的是 HttpClient 库</li>
<li>第三种初始化方法 使用的是 OkHttp 库</li>
</ol>
<p>所以如果对具体使用的类库有要求的话，应该在初始化时指定具体的工厂类，当然还需要添加相关的依赖</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.apache.httpcomponents&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;httpclient&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;4.5.3&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.squareup.okhttp3&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;okhttp&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;3.11.0&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure>



<hr>
<p>微信端的朋友也可关注我的公众号</p>
<p><strong>小马向前走</strong></p>
<p><img src="/2021/02/08/07-rest-template/qrcode-12cm.jpg" alt="qrcode-12cm"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="cn">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/11/16/06-util-usage-01/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="maning">
      <meta itemprop="description" content="小马向前走">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小马向前走">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/11/16/06-util-usage-01/" class="post-title-link" itemprop="url">工具类整理</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-11-16 00:00:00" itemprop="dateCreated datePublished" datetime="2020-11-16T00:00:00+08:00">2020-11-16</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2021-05-22 16:39:52" itemprop="dateModified" datetime="2021-05-22T16:39:52+08:00">2021-05-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>本文推荐一些java中好用的工具类</p>
<ol>
<li>如统计执行时长的 Guava下的 Stopwatch 类</li>
<li>Bean 与 Map 相互转换的 Apache的BeanUtils</li>
</ol>
<h1 id="计时相关"><a href="#计时相关" class="headerlink" title="计时相关"></a>计时相关</h1><p>项目中经常有记录某一操作花费多长时间的需求，如下是最开始的代码，是不是很熟悉？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">long</span> start = System.currentTimeMillis();</span><br><span class="line"><span class="comment">//do something</span></span><br><span class="line">Thread.sleep(<span class="number">2020</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> end = System.currentTimeMillis();</span><br><span class="line">System.out.println(<span class="string">&quot;运行时间：&quot;</span>+(end - start)+ <span class="string">&quot;ms&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>这种方式确实简单，但是这种方式也有不灵活的地方</p>
<ol>
<li>默认情况下我们只能取ms为单位，如果需要分钟，则需要自己换算单位。</li>
<li>不能支持更复杂的计时需求，如中途暂停计时，当代码执行到某一步再继续计时。</li>
</ol>
<p><strong>下面介绍一个关于计时的工具类 Guava下的 Stopwatch</strong></p>
<p>引入依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.google.guava<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>guava<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>23.5-jre<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>Stopwatch代码示例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">stopwatch1</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">    <span class="comment">//创建stopwatch并开始计时</span></span><br><span class="line">    Stopwatch stopwatch=Stopwatch.createStarted();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    Thread.sleep(<span class="number">2020</span>);</span><br><span class="line">    System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//暂停计时</span></span><br><span class="line">    stopwatch.stop();</span><br><span class="line">    Thread.sleep(<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//再次开始计时</span></span><br><span class="line">    stopwatch.start();</span><br><span class="line">    Thread.sleep(<span class="number">2020</span>);</span><br><span class="line">    stopwatch.stop();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//统计运行时长</span></span><br><span class="line">    System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到除了满足基本的计时需求外，其使用更灵活，更简单。 </p>
<ol>
<li><p>不需要自己再去计算时长</p>
</li>
<li><p>可以中途暂停计时</p>
</li>
</ol>
<p>下面总结下常用的操作</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//创建计时器,但是不立即运行 需要主动调用 stopwatch.start()方法开始计时</span></span><br><span class="line">Stopwatch stopwatch=Stopwatch.createUnstarted();</span><br><span class="line">stopwatch.start();</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建stopwatch并开始计时</span></span><br><span class="line"> Stopwatch stopwatch=Stopwatch.createStarted();</span><br><span class="line"></span><br><span class="line"><span class="comment">//暂停计时</span></span><br><span class="line">stopwatch.stop();</span><br><span class="line"></span><br><span class="line"><span class="comment">//统计运行时长</span></span><br><span class="line">stopwatch.elapsed(TimeUnit.MILLISECONDS)</span><br></pre></td></tr></table></figure>



<h1 id="Bean-与-Map-互转"><a href="#Bean-与-Map-互转" class="headerlink" title="Bean 与 Map 互转"></a>Bean 与 Map 互转</h1><p>日常开发中经常有将Bean转成Map,或者将Map转成Bean的需求,这里阐述下具体的实现.</p>
<p>目前Map 与 Bean互转有如下几种方式</p>
<ol>
<li>方式一: 通过Jackson 先将Bean转成json,再将json转成Map</li>
<li>方式二: 使用Spring的BeanMap类</li>
<li>方式三: 使用Apache的BeanUtils</li>
</ol>
<p>我目前使用方式三. 原因如下</p>
<ol>
<li>方式一 效率不行</li>
<li>方式二 需要依赖spring,在有些非spring系工程中不方便使用</li>
</ol>
<p><strong>使用Apache的BeanUtils</strong></p>
<p><strong>引入依赖</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>commons-beanutils<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>commons-beanutils<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.9.4<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p><strong>bean 转 map</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * bean 转 map</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Map <span class="title">beanToMap</span><span class="params">(Object bean)</span></span>&#123;</span><br><span class="line">    Map&lt;String,String&gt; map=Maps.newHashMap();</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        map=BeanUtils.describe(bean);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> map;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><strong>map 转 bean</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * map 转 bean</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Object <span class="title">mapToBean</span><span class="params">(Map map,Object bean)</span></span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        BeanUtils.populate(bean,map);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> bean;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>







<hr>
<p>微信端的朋友也可关注我的公众号</p>
<p><strong>小马向前走</strong></p>
<p><img src="/2020/11/16/06-util-usage-01/qrcode-12cm.jpg" alt="qrcode-12cm"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="cn">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/10/26/04-st4-usage/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="maning">
      <meta itemprop="description" content="小马向前走">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小马向前走">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/10/26/04-st4-usage/" class="post-title-link" itemprop="url">st4 字符串模板引擎的使用</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-10-26 09:42:58" itemprop="dateCreated datePublished" datetime="2020-10-26T09:42:58+08:00">2020-10-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h1><p>有时需要定义一些模板,然后用实际值替换占位符. 这里介绍一个jar包 <strong>ST4</strong>.</p>
<p>还是以生成验证码短信的例子为背景.</p>
<p>如下是我定义的模板,需要传入用户名和验证码. </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">你好 &lt;name&gt; , 你的验证码是 &lt;captcha&gt;</span><br><span class="line">验证码5分钟有效,尽快使用</span><br></pre></td></tr></table></figure>



<p>最终生成的文本如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">你好 张三 , 你的验证码是 123456</span><br><span class="line">验证码5分钟有效,尽快使用</span><br></pre></td></tr></table></figure>



<p><strong>如果模板越复杂使用ST4越能感觉到优势</strong>, 如用于生成源代码、web页面、电子邮件等复杂样式的文本.</p>
<p>github地址</p>
<p><a target="_blank" rel="noopener" href="https://github.com/antlr/stringtemplate4">https://github.com/antlr/stringtemplate4</a></p>
<h1 id="使用示例"><a href="#使用示例" class="headerlink" title="使用示例"></a>使用示例</h1><h2 id="pom-xml-引入依赖"><a href="#pom-xml-引入依赖" class="headerlink" title="pom.xml 引入依赖"></a>pom.xml 引入依赖</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.antlr<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>ST4<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.8<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="一些示例"><a href="#一些示例" class="headerlink" title="一些示例"></a>一些示例</h2><h3 id="示例1-简单示例"><a href="#示例1-简单示例" class="headerlink" title="示例1 简单示例"></a>示例1 简单示例</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">     * 最简单的示例</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">helloWorld</span><span class="params">()</span></span>&#123;</span><br><span class="line">        ST hello = <span class="keyword">new</span> ST(<span class="string">&quot;Hello, &lt;name&gt;&quot;</span>);</span><br><span class="line">        hello.add(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;World&quot;</span>);</span><br><span class="line">        System.out.println(hello.render());</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>这个例子算是st4的helloworld版,让你快速感受下.</p>
<h3 id="示例2-STGroupFile的使用"><a href="#示例2-STGroupFile的使用" class="headerlink" title="示例2 STGroupFile的使用"></a>示例2 STGroupFile的使用</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">     * 演示  STGroupFile 的使用</span></span><br><span class="line"><span class="comment">     * 可以将多个模板内容放在一个文件中,所以其后缀名叫stg.</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testGroupFile</span><span class="params">()</span></span>&#123;</span><br><span class="line">        STGroup group=<span class="keyword">new</span> STGroupFile(<span class="string">&quot;templates/templates.stg&quot;</span>);</span><br><span class="line">        ST st=group.getInstanceOf(<span class="string">&quot;ShortMessage&quot;</span>);</span><br><span class="line">        st.add(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;张三&quot;</span>);</span><br><span class="line">        st.add(<span class="string">&quot;captcha&quot;</span>,<span class="string">&quot;123456&quot;</span>);</span><br><span class="line">        String result=st.render();</span><br><span class="line">        System.out.println(result);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>



<p>如下定义模板文件, 可以看到其定义有点像函数的形式.</p>
<p>resouces/templates/templates.stg</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ShortMessage(name,captcha) ::&#x3D;</span><br><span class="line">&lt;&lt;</span><br><span class="line">你好 &lt;name&gt; , 你的验证码是 &lt;captcha&gt;</span><br><span class="line">验证码5分钟有效,尽快使用</span><br><span class="line">&gt;&gt;</span><br></pre></td></tr></table></figure>

<p>当模板过于复杂时,以硬编码的方式将模板写在代码中不方便,因此将模板内容写入到文件是一个不错的选择.</p>
<h3 id="示例3-传入参数是对象"><a href="#示例3-传入参数是对象" class="headerlink" title="示例3 传入参数是对象"></a>示例3 传入参数是对象</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    * 如下演示传入的参数是对象 而不是单个的值</span></span><br><span class="line"><span class="comment">    * 而且其占位符也不是默认的&lt; 和&gt; 而是$了</span></span><br><span class="line"><span class="comment">    * */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testModelObject</span><span class="params">()</span></span>&#123;</span><br><span class="line">        ST st = <span class="keyword">new</span> ST(<span class="string">&quot;&lt;b&gt;$u.id$&lt;/b&gt;: $u.name$&quot;</span>, <span class="string">&#x27;$&#x27;</span>, <span class="string">&#x27;$&#x27;</span>);</span><br><span class="line">        st.add(<span class="string">&quot;u&quot;</span>, <span class="keyword">new</span> User(<span class="number">999</span>, <span class="string">&quot;parrt&quot;</span>));</span><br><span class="line">        String result = st.render(); <span class="comment">// &quot;&lt;b&gt;999&lt;/b&gt;: parrt&quot;</span></span><br><span class="line">        System.out.println(result);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>这个例子也很经典 </p>
<ul>
<li>一是其传入的参数是一个对象,而不是字符串等基本类型了</li>
<li>二是这里自己指定了占位符</li>
</ul>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="cn">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/10/19/05-idea-plugin-recommend/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="maning">
      <meta itemprop="description" content="小马向前走">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小马向前走">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/10/19/05-idea-plugin-recommend/" class="post-title-link" itemprop="url">idea 插件推荐</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-10-19 11:20:59" itemprop="dateCreated datePublished" datetime="2020-10-19T11:20:59+08:00">2020-10-19</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2021-02-14 16:18:29" itemprop="dateModified" datetime="2021-02-14T16:18:29+08:00">2021-02-14</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">工具</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>本文推荐一些好用的idea插件,用好这些插件可以省好多时间.</p>
<h1 id="MyBatis-Log-Plugin"><a href="#MyBatis-Log-Plugin" class="headerlink" title="MyBatis Log Plugin"></a>MyBatis Log Plugin</h1><p>作用: 将mybatis打印到控制台的sql中的?替换成实际参数,替换后的sql可直接复制粘贴到数据库执行</p>
<p>推荐理由: 快速将mybatis打印出的日志拼接成实际的sql,在调试sql时很方便.</p>
<p><img src="/2020/10/19/05-idea-plugin-recommend/MyBatisLogPlugin-1.png" alt="MyBatisLogPlugin-1"></p>
<p>如下是生成的实际的sql</p>
<p><img src="/2020/10/19/05-idea-plugin-recommend/MyBatisLogPlugin-2.png" alt="MyBatisLogPlugin-2"></p>
<h1 id="Free-Mybatis-Plugin"><a href="#Free-Mybatis-Plugin" class="headerlink" title="Free Mybatis Plugin"></a>Free Mybatis Plugin</h1><p>作用:快速在mybatis的mapper接口类和xml间跳转</p>
<p>推荐理由: 从代码中快速跳转到mybatis的xml</p>
<p><img src="/2020/10/19/05-idea-plugin-recommend/FreeMybatisPlugin-1.png" alt="FreeMybatisPlugin-1"></p>
<h1 id="RestfulToolkit"><a href="#RestfulToolkit" class="headerlink" title="RestfulToolkit"></a>RestfulToolkit</h1><p>作用: 其会根据url查找controller</p>
<p>推荐理由: 通过url快速定位到controller, 方便快速定位到代码</p>
<p>快捷键: command+\ </p>
<p><img src="/2020/10/19/05-idea-plugin-recommend/RestfulToolkit-1.png" alt="image-20201019170120384"></p>
<h1 id="彩色括号-Rainbow-Bracket"><a href="#彩色括号-Rainbow-Bracket" class="headerlink" title="彩色括号 Rainbow Bracket"></a>彩色括号 Rainbow Bracket</h1><p>作用: 高亮显示括号配对</p>
<p>推荐理由: 有时代码中的括号直的很多,不好分清楚这行代码到底在哪个括号里,有了彩色括号方便太多了.</p>
<p><img src="/2020/10/19/05-idea-plugin-recommend/RainbowBrackets-1.png" alt="RainbowBrackets-1"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="cn">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/10/08/03-commons-text-usage/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="maning">
      <meta itemprop="description" content="小马向前走">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小马向前走">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/10/08/03-commons-text-usage/" class="post-title-link" itemprop="url">使用commons-text 进行占位符替换</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-10-08 14:36:02" itemprop="dateCreated datePublished" datetime="2020-10-08T14:36:02+08:00">2020-10-08</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-10-19 11:38:43" itemprop="dateModified" datetime="2020-10-19T11:38:43+08:00">2020-10-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="背景描述"><a href="#背景描述" class="headerlink" title="背景描述"></a>背景描述</h1><p>日常开发工作中会遇到替换占位符的需求</p>
<p>例如给用户发验证码短信时需要拼接如下的字符串   “张三 欢迎使用,您的验证码是123456” , 其中用户名和验证码需要动态拼接,其他字符串都是固定的.  则这种情况下可以考虑使用commons-text,比自己实现字符串替换要好.</p>
<h1 id="具体步骤"><a href="#具体步骤" class="headerlink" title="具体步骤"></a>具体步骤</h1><p>引入 commons-text 的依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.commons<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>commons-text<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.9<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>具体代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * 演示最简单的使用方式</span></span><br><span class="line"><span class="comment">        * 这里默认的替换符就是$&#123;&#125;</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        Map&lt;String, Object&gt; params = <span class="keyword">new</span> HashMap&lt;String, Object&gt;();</span><br><span class="line">        params.put(<span class="string">&quot;userName&quot;</span>, <span class="string">&quot;admin&quot;</span>);</span><br><span class="line">        params.put(<span class="string">&quot;captcha&quot;</span>, <span class="string">&quot;123456&quot;</span>);</span><br><span class="line">        StringSubstitutor stringSubstitutor=<span class="keyword">new</span> StringSubstitutor(params);</span><br><span class="line">        String template = <span class="string">&quot;$&#123;userName&#125; 欢迎使用,您的验证码是$&#123;captcha&#125;&quot;</span>;</span><br><span class="line">        String result=stringSubstitutor.replace(template);</span><br><span class="line">        System.out.println(result);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>




      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="cn">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/09/03/02-my-hello/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="maning">
      <meta itemprop="description" content="小马向前走">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小马向前走">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/03/02-my-hello/" class="post-title-link" itemprop="url">myHello</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-09-03 17:13:18 / Modified: 17:22:43" itemprop="dateCreated datePublished" datetime="2020-09-03T17:13:18+08:00">2020-09-03</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>兜兜转转, 又来到了这里, 欢迎回来</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="cn">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/09/03/01-hello-world/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="maning">
      <meta itemprop="description" content="小马向前走">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小马向前走">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/03/01-hello-world/" class="post-title-link" itemprop="url">Hello World</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-09-03 14:52:27" itemprop="dateCreated datePublished" datetime="2020-09-03T14:52:27+08:00">2020-09-03</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>Welcome to <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a target="_blank" rel="noopener" href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a target="_blank" rel="noopener" href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">&quot;My New Post&quot;</span></span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  


  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/"><i class="fa fa-angle-left" aria-label="Previous page"></i></a><a class="page-number" href="/">1</a><span class="page-number current">2</span>
  </nav>



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">maning</p>
  <div class="site-description" itemprop="description">小马向前走</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">18</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">maning</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
